package org.springframework.samples.countries.web.translate;

import java.util.List;
import java.util.Stack;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import pikes.ecma.Identifier;
import pikes.ecma.StringLiteral;

public class ConstantLoadOperation implements ByteCodeOperation {

	protected static final Log translatorLogger = LogFactory.getLog("Translator");

	private String byteCodeName = null;

	private Object constant = null;

	public ConstantLoadOperation(Object constant, String byteCodeName) {
		this.constant = constant;
		this.byteCodeName = byteCodeName;
	}

	public ConstantLoadOperation(Object constant) {
		this(constant, "LDC");
	}

	final public void execute(int numberOfParameters, Stack<Object> stack, List<String> localVariables) {
		translatorLogger.info(byteCodeName + ": Putting " + constant + " on stack");
		if (constant instanceof CharSequence) {
			CharSequence charSequence = (CharSequence) constant;
			stack.push(new StringLiteral(charSequence));
		}
		else {
			stack.push(new Identifier(constant.toString()));
		}

	}

	@Override
	public String toString() {
		return byteCodeName + " " + constant.toString();
	}

}